package org.openkoala.koala.deploy.dialog;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * 一个窗体，显示生成EJB或WS服务的提示信息
 * 
 * 同时提供打开文件的功能，加强用户体验
 * 
 * @author lingen
 * 
 */
public class FileOpenDialog {

	private Shell shell;

	private String path;
	
	private Button okButton;
	
	private Button openButton;
	
	public FileOpenDialog(Shell shell, String title,String path) {
		this.shell = shell;
		this.path = path;
		drawUI(title);
		initEvent();
	}
	

	private void drawUI(String title) {
		shell.setSize(500, 150);
		shell.setLayout(new FormLayout());
		shell.setText(title);
		Label label = new Label(shell, SWT.NONE);
		label.setImage(getInfoImage());
		FormData data = new FormData();
		 data.left = new FormAttachment(0,10);
		 data.top = new FormAttachment(0,10);
		 data.right = new FormAttachment(label,60);
		 data.bottom = new FormAttachment(label,60);
		label.setLayoutData(data);
		Text text = new Text(shell,SWT.MULTI|SWT.WRAP|SWT.READ_ONLY);
		
		text.setText("生成成功，路径位于:\n\r"+path);
		
		data = new FormData();
		data.left = new FormAttachment(label,20);
		data.top = new FormAttachment(0,10);
		data.right = new FormAttachment(text,400);
		data.bottom = new FormAttachment(text,60);
		text.setLayoutData(data);
		
		openButton = new Button(shell,SWT.PUSH);
		openButton.setText("打开文件夹");
		
		data = new FormData();
		data.left = new FormAttachment(50,0);
		data.top = new FormAttachment(60,10);
		data.right = new FormAttachment(openButton,100);
		data.bottom = new FormAttachment(openButton,30);
		openButton.setLayoutData(data);
		
		String osName = System.getProperty("os.name");
		if(osName.toLowerCase().contains("windows")==false){
			openButton.setVisible(false);
		}
		
		okButton = new Button(shell,SWT.PUSH);
		okButton.setText("关闭");
		data = new FormData();
		data.left = new FormAttachment(openButton,10);
		data.top = new FormAttachment(openButton,0,SWT.TOP);
		data.right = new FormAttachment(okButton,100);
		data.bottom = new FormAttachment(okButton,30);
		okButton.setLayoutData(data);
	}
	
	public void open(){
		this.shell.open();
	}
	
	private void initEvent(){
		this.okButton.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}});
		
		this.openButton.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					String parentPath = path.substring(0,path.lastIndexOf("/"));
					if(parentPath.startsWith("/"))parentPath = parentPath.substring(1);
					parentPath = parentPath.replaceAll("/", "\\\\");
					Runtime.getRuntime().exec("explorer "+parentPath);
					shell.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				
			}});
	}

	public Image getInfoImage() {
		return getSWTImage(SWT.ICON_INFORMATION);
	}

	private Image getSWTImage(final int imageID) {
		final Display display;
		display = Display.getCurrent();

		final Image[] image = new Image[1];
		display.syncExec(new Runnable() {
			public void run() {
				image[0] = display.getSystemImage(imageID);
			}
		});
		
		return image[0];
	}

	public static void main(String args[]) {
//		Display display = new Display();
//		Shell shell = new Shell(display, SWT.CLOSE);
//
//		FileOpenDialog dialog = new FileOpenDialog(shell, "test","/C:/USERS/lingen.liu/workspace/runtime-EclipseApplication/projectArftifactId/target/");
//		shell.open();
//		while (!shell.isDisposed()) {
//			if (!display.readAndDispatch()) {
//				display.sleep();
//			}
//		}
//		display.dispose();
		
		String osName = System.getProperty("os.name");
		System.out.println(osName);
	}

}
